package net.tv90.bfmccore.game;

import net.tv90.bfmccore.game.impl.ConquestGame;
import net.tv90.bfmccore.game.view.GameViewManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Arrays;

import static net.tv90.bfmccore.game.GameManager.waitingPlayers;

public class GameCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("game")) {
            if (args.length == 0) {
                sender.sendMessage("/game rd");
                return true;
            }
            if (args[0].equalsIgnoreCase("rd")) {
                if (sender instanceof Player) {
                    GameManager.getInstance().currentGame.joinGame((Player) sender);
                    return true;
                }
            }
            if (args[0].equalsIgnoreCase("next")) {
                if (sender instanceof Player && !sender.isOp()) return false;
                GameManager.getInstance().currentGame.currentTime = 30;
                GameManager.getInstance().currentGame.executeEnd = true;
                return true;
            }
            if (args[0].equalsIgnoreCase("next2")) {
                if (sender instanceof Player && !sender.isOp()) return false;
                GameManager.getInstance().currentGame.firstFactionTicket = 5;
                GameManager.getInstance().currentGame.secondFactionTicket = 5;
                return true;
            }
            if (args[0].equalsIgnoreCase("deployv")) {
                if (sender instanceof Player && !sender.isOp()) return false;
                GameBase gm = GameManager.getInstance().currentGame;
                if (gm instanceof ConquestGame) {
                    System.out.println(Arrays.toString(args));
                    ((ConquestGame) gm).teleportVehicle(Bukkit.getPlayer(args[1]), args[2], args[3], args[4]);
                }
                return true;
            }
            if (args[0].equalsIgnoreCase("vote") || args[0].equalsIgnoreCase("v")) {
                if (sender instanceof Player) {
                    try {
                        int index = Integer.parseInt(args[1]);
                        GameManager.getInstance().voteGame((Player) sender, index);
                    } catch (Exception e) {
                        sender.sendMessage("§e[管理员]请输入正确的指令 §b/game v <序号> §e，如 §b/game v 0 §e给0号地图投票");
                        return false;
                    }
                    return true;
                }
            }
        }
        return false;
    }
}
